{% extends 'base.html' %}

{% block content %}
<h2>学生列表</h2>
<!-- 添加学生按钮 -->
<a href="{{ url_for('main.student_add') }}" class="btn btn-primary mb-3">添加学生</a>

<!-- 学生列表表格 -->
<table class="table table-striped">
    <thead>
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>班级</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
    {% for student in students %}
        <tr>
            <td>{{ student.student_id }}</td>
            <td>{{ student.name }}</td>
            <td>{{ student.class_name }}</td>
            <td>
                <a href="{{ url_for('main.student_edit', id=student.id) }}" class="btn btn-sm btn-secondary">编辑</a>
                <form action="{{ url_for('main.student_delete', id=student.id) }}" method="post" style="display:inline;">
                    <button type="submit" class="btn btn-sm btn-danger" onclick="return confirm('确认删除该学生及其相关成绩吗？');">删除</button>
                </form>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

<!-- 分页 -->
<nav aria-label="Page navigation">
  <ul class="pagination">
    {% if pagination.has_prev %}
      <li class="page-item"><a class="page-link" href="{{ url_for('main.student_list', page=pagination.prev_num) }}">上一页</a></li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">上一页</span></li>
    {% endif %}
    {% for page_num in pagination.iter_pages(left_edge=2, right_edge=2, left_current=2, right_current=2) %}
      {% if page_num %}
        {% if page_num == pagination.page %}
          <li class="page-item active"><span class="page-link">{{ page_num }}</span></li>
        {% else %}
          <li class="page-item"><a class="page-link" href="{{ url_for('main.student_list', page=page_num) }}">{{ page_num }}</a></li>
        {% endif %}
      {% else %}
        <li class="page-item disabled"><span class="page-link">…</span></li>
      {% endif %}
    {% endfor %}
    {% if pagination.has_next %}
      <li class="page-item"><a class="page-link" href="{{ url_for('main.student_list', page=pagination.next_num) }}">下一页</a></li>
    {% else %}
      <li class="page-item disabled"><span class="page-link">下一页</span></li>
    {% endif %}
  </ul>
</nav>
{% endblock %}
